package com.example.demo.controller;

import com.example.demo.common.zookeeper.WatcherApi;
import com.example.demo.common.zookeeper.ZkApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/zookeeper")
public class ZookeeperController {
    //@Autowired
    private ZkApi zkApi;

    @GetMapping(value = "createNode")
    public boolean createNode(String path, String data) {
        log.debug("ZookeeperController create node {},{}", path, data);
        return zkApi.createNode(path, data);
    }

    @GetMapping(value = "getNode")
    public List<String> getNode(String path) throws InterruptedException, KeeperException {
        log.debug("ZookeeperController get node {}", path);
        return zkApi.getChildren(path);
    }

    @GetMapping(value = "getNodeData")
    public String getNodeData(String path) throws InterruptedException, KeeperException {
        log.debug("ZookeeperController get node data {}", path);
        return zkApi.getData(path, new WatcherApi());
    }
}
